package org.codefinger.dao.callback;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codefinger.dao.impl.ResultSetCallback;
import org.codefinger.dao.util.jdbc.JDBCUtil;

public class MapListQueryCallback implements ResultSetCallback<List<Map<String, Object>>> {

	public static final MapListQueryCallback	INSTANCE	= new MapListQueryCallback();

	private MapListQueryCallback() {
		super();
	}

	@Override
	public List<Map<String, Object>> getResult(ResultSet resultSet, String[] aliases) throws SQLException {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map;
		ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
		int i;
		while (resultSet.next()) {
			i = 1;
			map = new HashMap<String, Object>();
			for (String alias : aliases) {
				map.put(alias, JDBCUtil.getResult(resultSet, i, resultSetMetaData.getColumnType(i++)));
			}
			list.add(map);
		}
		return list;
	}

}
